package com.cssw.fyzb.busi.api.user;


import com.cssw.fyzb.busi.cache.SmsTpls;
import com.cssw.fyzb.busi.config.TpsClient;
import com.cssw.fyzb.busi.service.system.SmsTplService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.busi.util.BizUtil;
import com.cssw.fyzb.busi.util.VerifyUtil;
import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.core.model.dodb.BusiSmsTpl;
import com.cssw.fyzb.core.model.dodb.User;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplecache.SimpleCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.cssw.fyzb.busi.cache.CacheKeys.forVerify;
import static com.cssw.fyzb.busi.util.Bizs.UNKNOWN_TRADE;
import static com.cssw.fyzb.busi.util.Bizs.USER_NOT_EXISTS;

/**
 * 获取短信验证码
 *
 * @author Feng Chen
 */
@Function(description = "获取短信验证码", parameters = {
        @Parameter(name = "mobile", type = Type.String, description = "手机号码", required = true),
        @Parameter(name = "trade", type = Type.String, description = "业务类型 BT010：注册, BT011:登录, BT012:重置密码, BT013: 解绑设备, BT015 重置交易密码", required = true)
})
@Component
public class GetVerify implements Action {
    @Autowired
    private SimpleCache simpleCache;
    @Autowired
    private TpsClient tpsClient;
    @Autowired
    private UserService userService;
    @Autowired
    private SmsTplService smsTplService;

    private static final Logger logger = LoggerFactory.getLogger(GetVerify.class);

    @Override
    public Response execute(Request request) throws Exception {
        String mobile = request.getString("mobile");
        String trade = request.getString("trade");
        System.out.println(mobile);
        System.out.println(trade);
        try {
            // 非注册业务判断手机号是否存在
            if (!"BT010".equals(trade)) {
                User user = userService.getByMobile(mobile);
                if (user == null) {
                    throw new BizException(USER_NOT_EXISTS.code, USER_NOT_EXISTS.message);
                }
            }

            String verifyCode = VerifyUtil.get();
            simpleCache.put(forVerify(mobile), verifyCode, 5 * 60);

            // 发送短信
            sendSms(mobile, trade, verifyCode);
            return Response.success();
        } catch (Exception e) {
            if (e instanceof BizException) {
                BizException be = (BizException) e;
                return Response.fail(be.getErrMsg(), be.getErrCode());
            }
            logger.error("", e);
            return Response.fail("获取短信验证码失败");
        }
    }

    private void sendSms(String mobile, String trade, String verifyCode) {
        BusiSmsTpl busiSmsTpl = SmsTpls.getTpl(trade);
        if (busiSmsTpl == null) {
            BizUtil.exception(UNKNOWN_TRADE);
        }
        Request request = new Request("SendSms");
        request.setParameter("mobile", mobile);
        request.setParameter("message", busiSmsTpl.getContent().replace("##code##", verifyCode));
        Response response = tpsClient.send(request);
        if (!response.isSuccess()) {
            throw new BizException(response.getCode(), response.getMessage());
        }
    }

}
